<?php 
namespace XFCore\traits\user;
use lib\{Examine};
trait OtherTrait{
    public function workgetDetail(){
        if($this->islogin_user != 1 ){
            $this->echomsg('json',"请先登录");
        }
        $id = $this->input->id;
        if(empty($id)){
            exitjson(-1,'非法操作！');
        }
        $row = $this->db->workrow($id,'id','`num`,`types`,`biaoti`,`text`,`edata`,`huifu`,`wdata`,`active`');
        if(empty($row)){
            $this->echomsg('json',"工单数据不存在");
        }
        $row['status'] = $row['active'] == '1' ? '已完结':"待处理";
        exitjson(200,'success',['data'=>$row]);
    }
    public function submitTicket($get , $data){
        if($this->islogin_user != 1){
            $this->echomsg('json',"请先登录");
        }
        $requiredFields = ['biaoti', 'types', 'text'];
        foreach ($requiredFields as $field){
            if (!isset($data[$field]) || empty($data[$field])){
                $this->echomsg('json',"参数不全");
            }
        }
        $params = array_filter($data, fn($v) => $v !== '');
        Examine::checkRequiredParams($params);
        $rowdata = [
            'active'=>0,
            'num'=>rand(100000000,999999999),
            'uid'=>$this->uid,
            'edata'=>$this->date,
            'qq'=>$this->userrow['qq'],
        ];
        $params = array_merge($params,$rowdata);
        
        if($this->conf->work_zt == 0){
            $this->echomsg('json','管理员未开启工单');
        }
        if(isset($_SESSION['work_submit']) && $_SESSION['work_submit']>time()-2400){
            $this->echomsg('json','请勿频繁提交工单');
        }
        
        if($this->db->insert('work',$params)){
            $_SESSION['work_submit']=time();
            if($this->conf->mail_recv){
                $msg = $this->MsgNotice->send('userwork',"管理员",$params);
                $sub = $this->conf['sitename'].' - 您有新的工单要处理';
                send_mail($this->conf->mail_recv, $sub, $msg);
            }
            exitjson(200,'发起工单成功');
        }
        exitjson(-1,'发起失败！'); 
    }
    public function edit_codename(){
        if($this->islogin_user != 1){
            $this->echomsg('json',"请先登录");
        }
        $codename = $this->input->codename;
        if(empty($codename)){
            exitjson(-1,'自定义名称不可为空！');
        }
        if($this->db->update('user',['codename'=>$codename],['pid'=>$this->uid])){
            exitjson(200,'修改成功！');
        }
        exitjson(-1,'修改失败！');
    }
}